package com.zd.service;

import com.cloopen.rest.sdk.BodyType;
import com.cloopen.rest.sdk.CCPRestSmsSDK;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class SmsService {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    //生产环境请求地址：app.cloopen.com
    static String serverIp = "app.cloopen.com";
    //请求端口
    static String serverPort = "8883";
    //主账号,登陆云通讯网站后,可在控制台首页看到开发者主账号ACCOUNT SID和主账号令牌AUTH TOKEN
    static String accountSId = "2c94811c8a27cf2d018b0de7c62136f3";
    static String accountToken = "cf8b65c272404accba88b2b2f4453d69";
    //请使用管理控制台中已创建应用的APPID
    static String appId = "2c94811c8a27cf2d018b0de7c78e36fa";


    //发送短信验证码
    public int sendMessage(String phone) {

        int resultNum = 0;

        //4位验证码
        Random random = new Random();
        StringBuffer code = new StringBuffer();
        for (int i = 0; i < 4; i++) {
            int num = random.nextInt(10);
            //第一位不能为0
            while (num==0 && i==0){
                num = random.nextInt(10);
            }
            System.out.println("num = " + num);
            code.append(num);
        }

        String codeStr = code.toString();

        //此处仅仅生成四位验证码
        System.out.println("codeStr = " + codeStr);


        //将验证码存储到redis数据库中
        Map<String,Object> map = new HashMap<>();
        map.put("codeStr",codeStr);

        redisTemplate.boundValueOps(phone).set(map,1, TimeUnit.MINUTES);

        CCPRestSmsSDK sdk = new CCPRestSmsSDK();
        sdk.init(serverIp, serverPort);
        sdk.setAccount(accountSId, accountToken);
        sdk.setAppId(appId);
        sdk.setBodyType(BodyType.Type_JSON);
        //短信模板id 默认测试环境的id是 1
        String templateId= "1";
        //占位符传参，参数1：4位验证码，参数2：验证码逻辑有效期时长
        String[] datas = {codeStr,"5"};
//        String subAppend="1234"; //可选 扩展码，四位数字 0~9999
//        String reqId="fadfafas"; //可选 第三方自定义消息id，最大支持32位英文数字，同账号下同一自然天内不允许重复

        //HashMap<String, Object> result = sdk.sendTemplateSMS(to,templateId,datas);
        HashMap<String, Object> result = sdk.sendTemplateSMS(phone,templateId,datas);
        if("000000".equals(result.get("statusCode"))){
            System.out.println("验证码发送成功！");
            resultNum = 1;
            //正常返回输出data包体信息（map）
//            HashMap<String,Object> data = (HashMap<String, Object>) result.get("data");
//            Set<String> keySet = data.keySet();
//            for(String key:keySet){
//                Object object = data.get(key);
//                System.out.println(key +" = "+object);
//            }
        }else{
            System.out.println("验证码发送失败！");
            //异常返回输出错误码和错误信息
            System.out.println("错误码=" + result.get("statusCode") +" 错误信息= "+result.get("statusMsg"));
        }

        return resultNum;

    }


    // 检验验证码方法
    public boolean verifyCode(String inputCode, String expectedCode) {
        return inputCode.equals(expectedCode);
    }


}
